{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain_community.document_loaders import JSONLoader\n",
    "import json\n",
    "from pathlib import Path"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain_community.document_loaders import JSONLoader\n",
    "\n",
    "loader = JSONLoader(\n",
    "    file_path=\"../Data/JD_chat.json\",\n",
    "    jq_schema='.[] | { \"userID\": .[\"user_id\"], \"QA\": .[\"content\"] }',\n",
    "    #jq_schema='.[] |  .[\"留言内容\"]',\n",
    "    text_content=False,\n",
    "    json_lines=False,\n",
    ")\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "docs = loader.load()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "page_content='{\"userID\": \"USERID_10003667\", \"QA\": \"user:\\t\\u4ec0\\u4e48\\u65f6\\u5019\\u80fd\\u53d1\\u8d27\\u554a\\nuser:\\t\\u4ec0\\u4e48\\u65f6\\u5019\\u80fd\\u53d1\\u8d27\\u554a\\nuser:\\t\\u5f97\\u7b49\\u591a\\u4e45\\u554a\\nuser:\\t\\u6211\\u5bb6\\u72d7\\u72d7\\u7b49\\u7740\\u5403\\u996d\\u5462\\nuser:\\t?\\nwaiter:\\t\\u8bf7\\u60a8\\u7a0d\\u7b49\\u4e00\\u4e0b\\uff0c\\u6b63\\u5728\\u4e3a\\u60a8\\u6838\\u5b9e\\u5904\\u7406\\u4e2d\\u54e6~\\nwaiter:\\t\\u6709\\u4ec0\\u4e48\\u95ee\\u9898\\u6211\\u53ef\\u4ee5\\u5e2e\\u60a8\\u5904\\u7406\\u6216\\u89e3\\u51b3\\u5462?\\nwaiter:\\t[\\u6570\\u5b57x] \\u8010\\u5a01\\u514b(Navarch) \\u5ba0\\u7269\\u5929\\u7136\\u7cae[\\u59d3\\u540dx]\\u72ac\\u6210\\u72ac\\u72d7\\u7cae[\\u6570\\u5b57x]kg\\nuser:\\t\\u5bf9\\u7684\\u3002\\nwaiter:\\t\\u8fd9\\u6b3e\\u5546\\u54c1\\u5e93\\u623f\\u8fd8\\u6ca1\\u6709\\u5230\\u8d27\\u2019\\nuser:\\t\\u4ec0\\u4e48\\u65f6\\u5019\\u80fd\\u5230\\u8d27\\u554a?\\nwaiter:\\t\\u53ef\\u4ee5\\u7ed9\\u60a8\\u64cd\\u4f5c\\u6709\\u8d27\\u7684\\u5148\\u53d1\\nuser:\\t\\u96f6\\u98df\\u90a3\\u4e2a\\u6709\\u8d27\\u5457?\\nuser:\\t\\u72d7\\u7cae\\u4e00\\u888b\\u90fd\\u6ca1\\u6709\\u4e48?\\nuser:\\t\\u6709\\u7684\\u8bdd\\u90fd\\u7ed9\\u6211\\u5148\\u53d1\\u5427\\u3002\\nuser:\\t?\\nwaiter:\\t\\u7a0d\\u7b49\\u54c8\\nuser:\\t\\u6069\\nwaiter:\\t\\u67e5\\u8be2\\u4ee5\\u64cd\\u4f5c\\u4e86\\u6709\\u8d27\\u5148\\u53d1\\nwaiter:\\t\\u60a8\\u7684\\u8ba2\\u5355\\u73b0\\u5728\\u662f\\u4e24\\u4e2a\\u8ba2\\u5355\\nwaiter:\\t\\u5efa\\u8bae\\u60a8\\u5173\\u6ce8\\u4e00\\u4e0b\\u8ba2\\u5355\\u7684\\u7269\\u6d41\\u4fe1\\u606f\\u54c8\\nuser:\\t\\u72d7\\u7cae\\u5565\\u65f6\\u5019\\u80fd\\u6709\\u8d27?\\nwaiter:\\t\\u9884\\u8ba1\\u4e00\\u5468\\u5de6\\u53f3\\nuser:\\t\\u597d\\u5427\\uff0c\\u5e0c\\u671b\\u5feb\\u70b9\\u53d1\\u8d27\\nwaiter:\\t\\u597d\\u7684\\u4eb2\\nwaiter:\\t\\u8bf7\\u95ee\\u8fd8\\u6709\\u5176\\u4ed6\\u8fd8\\u53ef\\u4ee5\\u5e2e\\u5230\\u60a8\\u7684\\u5417?\\nuser:\\t\\u6ca1\\u4e86\\n\"}' metadata={'source': 'C:\\\\Users\\\\zzy\\\\Desktop\\\\RUC file\\\\LLM_abstract\\\\llm-isg-master\\\\TestCode\\\\Data\\\\JD_chat.json', 'seq_num': 2}\n"
     ]
    }
   ],
   "source": [
    "print(docs[1])\n",
    "#这里输出的是转义字符，但直接丢大模型也能识别出来内容，水平太菜不会修成中文的"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "c:\\Users\\zzy\\AppData\\Local\\Programs\\Python\\Python312\\Lib\\site-packages\\langchain_community\\llms\\openai.py:255: UserWarning: You are trying to use a chat model. This way of initializing it is no longer supported. Instead, please use: `from langchain_community.chat_models import ChatOpenAI`\n",
      "  warnings.warn(\n",
      "c:\\Users\\zzy\\AppData\\Local\\Programs\\Python\\Python312\\Lib\\site-packages\\langchain_community\\llms\\openai.py:1086: UserWarning: You are trying to use a chat model. This way of initializing it is no longer supported. Instead, please use: `from langchain_community.chat_models import ChatOpenAI`\n",
      "  warnings.warn(\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "# 加载 llm 模型\n",
    "from langchain_community.chat_models import ChatOpenAI\n",
    "os.environ[\"OPENAI_API_KEY\"] = 'sk-81bf128c90424cd5bec2c9a3c54ef309'\n",
    "os.environ[\"OPENAI_BASE_URL\"] = \"https://dashscope.aliyuncs.com/compatible-mode/v1\"\n",
    "from langchain.llms import OpenAI\n",
    "\n",
    "llm = OpenAI(model_name=\"gpt-3.5-turbo\",max_tokens=1024)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 定义一个函数，接受page_content作为参数\n",
    "import json\n",
    "def generate_summary_from_page_content(page_content):\n",
    "    # 解析page_content以获取query和answer\n",
    "\n",
    "    content_json = json.loads(page_content.page_content)\n",
    "    userID = content_json['userID']\n",
    "    QA = content_json['QA']\n",
    "\n",
    "    # 准备输入消息\n",
    "    messages = [\n",
    "        {\n",
    "            \"role\": \"system\",\n",
    "            \"content\": \"请根据以下查询和回答生成一条摘要，格式为json，要求包含{“用户id”}和{“用户需求”}和{“解决方式”}。\\\n",
    "            用户需求应该能够概括用户在这轮对话中有什么需求，解决方式应该概括客服是怎么解决用户的疑惑/需求的\\\n",
    "                将你的响应格式化为包含{“用户id”}和{“用户需求”}和{“解决方式”} 为键的 JSON 对象。\"\n",
    "        },\n",
    "        {\n",
    "            \"role\": \"user\",\n",
    "            \"content\": userID # 用户的查询\n",
    "        },\n",
    "        {\n",
    "            \"role\": \"assistant\",\n",
    "            \"content\": QA\n",
    "        }\n",
    "    ]\n",
    "\n",
    "    # 请求大模型生成摘要\n",
    "    response = client.chat.completions.create(\n",
    "        model=\"qwen-max\",  # 指定使用的模型名称\n",
    "        messages=messages,  # 定义的消息列表\n",
    "        max_tokens=1024,  # 限制最大令牌数为150\n",
    "        response_format={\n",
    "            \"type\": \"text\",  # 响应类型为纯文本\n",
    "        }\n",
    "    )\n",
    "\n",
    "    # 返回响应内容\n",
    "    return response.choices[0].message.content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "数据已成功添加到文件。\n",
      "数据已成功添加到文件。\n",
      "数据已成功添加到文件。\n",
      "数据已成功添加到文件。\n",
      "数据已成功添加到文件。\n"
     ]
    }
   ],
   "source": [
    "from openai import OpenAI\n",
    "import json\n",
    "client = OpenAI()\n",
    "\n",
    "for i in range(1, 6):  # 从docs[1]到docs[30]\n",
    "    page_content = docs[i]\n",
    "    summary_response = generate_summary_from_page_content(page_content)\n",
    "    json_data = convert_to_json(summary_response)\n",
    "    # 调用函数，将json_data添加到chatlogsummary.json文件中\n",
    "    append_to_json_file(json_data)\n",
    "    \n",
    "# page_content = docs[1]\n",
    "# summary_response = generate_summary_from_page_content(page_content)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "转换为JSON格式成功：\n",
      "{'用户id': 'USERID_10003667', '用户需求': '希望尽快发货，特别是狗粮和零食。', '解决方式': '客服查询了库存情况，并将有货的商品先发出。对于暂时无货的狗粮，客服告知预计一周左右到货，并建议用户关注订单物流信息。'}\n"
     ]
    }
   ],
   "source": [
    "import json\n",
    "\n",
    "\n",
    "def convert_to_json(json_str):\n",
    "    try:\n",
    "        # 清理Markdown代码块标记\n",
    "        cleaned_str = json_str.strip()  # 去除首尾空白字符\n",
    "        cleaned_str = cleaned_str.replace('```json', '', 1)  # 去除第一次出现的```json\n",
    "        cleaned_str = cleaned_str.replace('```', '', 1)  # 去除第一次出现的```\n",
    "        #具体如何清除多余的非json内容还得看后续生成的摘要是怎么样的\n",
    "        # 尝试将清理后的字符串解析为JSON对象\n",
    "        return json.loads(cleaned_str)\n",
    "    except json.JSONDecodeError as e:\n",
    "        # 如果解析失败，打印错误信息并返回None\n",
    "        print(f\"解析JSON时出错：{e}\")\n",
    "        return None\n",
    "\n",
    "\n",
    "# # 调用函数并打印结果\n",
    "# json_data = convert_to_json(summary_response)\n",
    "# if json_data is not None:\n",
    "#     print(\"转换为JSON格式成功：\")\n",
    "#     print(json_data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "数据已成功添加到文件。\n"
     ]
    }
   ],
   "source": [
    "import json\n",
    "import os\n",
    "\n",
    "def append_to_json_file(json_data, filename='chatlogsummary.json'):\n",
    "    try:\n",
    "        # 尝试读取文件中的现有内容\n",
    "        if os.path.exists(filename):\n",
    "            with open(filename, 'r', encoding='utf-8') as file:\n",
    "                data = json.load(file)\n",
    "        else:\n",
    "            # 如果文件不存在，则创建一个空列表\n",
    "            data = []\n",
    "\n",
    "        # 将新的json_data添加到列表中\n",
    "        data.append(json_data)\n",
    "\n",
    "        # 将更新后的内容写回文件\n",
    "        with open(filename, 'w', encoding='utf-8') as file:\n",
    "            json.dump(data, file, ensure_ascii=False, indent=4)\n",
    "        print(\"数据已成功添加到文件。\")\n",
    "    except json.JSONDecodeError as e:\n",
    "        print(f\"读取文件时发生错误：{e}\")\n",
    "    except Exception as e:\n",
    "        print(f\"写入文件时发生错误：{e}\")\n",
    "\n",
    "# 调用函数，将json_data添加到chatlogsummary.json文件中\n",
    "# append_to_json_file(json_data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "用户ID USERID_10003667 的历史对话摘要：\n",
      "{'用户id': 'USERID_10003667', '用户需求': '希望尽快发货，特别是狗粮和零食。', '解决方式': '客服查询了库存情况，并将有货的商品先发出。对于暂时无货的狗粮，客服告知预计一周左右到货，并建议用户关注订单物流信息。'}\n",
      "{'用户id': 'USERID_10003667', '用户需求': '希望尽快发货，尤其是狗粮和零食。', '解决方式': '客服核实了库存情况，并将有货的商品先发给用户。对于暂时无货的狗粮，预计一周左右到货，建议用户关注订单物流信息。'}\n"
     ]
    }
   ],
   "source": [
    "import json\n",
    "import os\n",
    "\n",
    "\n",
    "def find_user_chatlog_by_id(filename, user_id):\n",
    "    # 检查文件是否存在\n",
    "    if not os.path.exists(filename):\n",
    "        print(f\"文件{filename}不存在。\")\n",
    "        return None\n",
    "\n",
    "    try:\n",
    "        # 读取文件中的所有内容\n",
    "        with open(filename, 'r', encoding='utf-8') as file:\n",
    "            data = json.load(file)\n",
    "\n",
    "        # 过滤出与给定用户ID匹配的对话摘要\n",
    "        user_chatlogs = [entry for entry in data if entry.get(\"用户id\") == user_id]\n",
    "\n",
    "        # 如果没有找到匹配的对话摘要，输出提示信息\n",
    "        if not user_chatlogs:\n",
    "            print(\"用户近期没有对话内容\")\n",
    "            return None\n",
    "\n",
    "        # 返回查询结果\n",
    "        return user_chatlogs\n",
    "    except json.JSONDecodeError as e:\n",
    "        print(f\"读取文件时发生错误：{e}\")\n",
    "        return None\n",
    "    except Exception as e:\n",
    "        print(f\"处理文件时发生错误：{e}\")\n",
    "        return None\n",
    "\n",
    "\n",
    "filename = 'chatlogsummary.json'\n",
    "user_id = 'USERID_10003667'#这里以后会有一个函数是获取用户的id，也就是坐席agent进入的时候获取到用户的id\n",
    "# 调用函数，查找对应用户的历史对话摘要\n",
    "user_chatlogs = find_user_chatlog_by_id(filename, user_id)\n",
    "# 打印查询结果\n",
    "if user_chatlogs is not None:\n",
    "    print(f\"用户ID {user_id} 的历史对话摘要：\")\n",
    "    for chatlog in user_chatlogs:\n",
    "        print(chatlog)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
